//
//  LKManagerAllianceSearchViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/17.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKManagerAllianceSearchViewController.h"
#import "LKManagerAlianceListCell.h"
#import "LKAllTrendsViewController.h"
#import "LKOpenShopInforDataViewController.h"
@interface LKManagerAllianceSearchViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)NSMutableDictionary *paramDic;
@property(nonatomic,copy)NSString *currentStr;
@end

@implementation LKManagerAllianceSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentStr = @"";
    self.backButton.hidden = YES;
    [self createTableview];
    // Do any additional setup after loading the view.
}


-(void)createTableview{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKManagerAlianceListCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = viewDefualtColor;
    [self.view addSubview:_tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(-10);
            make.bottom.equalTo(self.view).offset(0);
        }];
    } else {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(-10);
            make.bottom.equalTo(self.view).offset(0);
        }];
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableView reloadData];
}
/// 上拉刷新
-(void)headerReloadData{
    self.page = 0;
    [self getListDataWithStatus:[self.type integerValue] AndWithpage:self.page andWords:self.currentStr];
//    self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"sort_mode":self.priceType,@"page":@(self.page)}];
//    [self getShopListDataWithParam:self.paramDic];
}

/// 下拉加载更多
-(void)footerReloadData{
    self.page++;
    [self getListDataWithStatus:[self.type integerValue] AndWithpage:self.page andWords:self.currentStr];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKManagerAlianceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKManagerAlianceListCell"];
    cell.model = self.dataArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 118.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LkMyShopOpenStatusListModel *model = self.dataArray[section];
    UIView *header = [[UIView alloc] init];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectMake(20, 0, WIDTH, 32) text:[NSString timestampSwitchTime:[model.add_time integerValue] andFormatter:@"YYYY-MM-dd"] textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] textAlignment:NSTextAlignmentLeft];
    [header addSubview:headerLabel];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32.f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.type = [[NSUserDefaults standardUserDefaults] objectForKey:@"shopTypeValue"];
    LkMyShopOpenStatusListModel *model = self.dataArray[indexPath.section];
    if ([self.type isEqualToString:@"1"] ) {
        if ([model.agent_type_id isEqualToString:@"2"]) {
            LKAllTrendsViewController *allTrendsVc = [[LKAllTrendsViewController alloc] init];
            allTrendsVc.mid = model.m_id;
            [self.presentingViewController.navigationController pushViewController:allTrendsVc animated:YES];
        }else{
            LKOpenShopInforDataViewController *vc = [[LKOpenShopInforDataViewController alloc] init];
            vc.uid = model.Id;
            vc.showBottomBtn = YES;
            [self.presentingViewController.navigationController pushViewController:vc animated:YES];
        }
    }else if ([self.type isEqualToString:@"2"]){
        if ([model.agent_type_id isEqualToString:@"2"]) {
            LKAllTrendsViewController *allTrendsVc = [[LKAllTrendsViewController alloc] init];
            allTrendsVc.mid = model.m_id;
            [self.presentingViewController.navigationController pushViewController:allTrendsVc animated:YES];
        }else{
            LKOpenShopInforDataViewController *vc = [[LKOpenShopInforDataViewController alloc] init];
            vc.uid = model.Id;
            [self.presentingViewController.navigationController pushViewController:vc animated:YES];
        }
    }else if([self.type isEqualToString:@"3"]){
        LKAllTrendsViewController *allTrendsVc = [[LKAllTrendsViewController alloc] init];
        allTrendsVc.mid =model.Id;
        
        [self.presentingViewController.navigationController pushViewController:allTrendsVc animated:YES];
    }
    
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
/// 根据状态获取商户信息
/// @param status 商户进件状态
-(void)getListDataWithStatus:(NSInteger)status AndWithpage:(NSInteger)page andWords:(NSString *)word{
    [SVProgressHUD show];
    self.type = [[NSUserDefaults standardUserDefaults] objectForKey:@"shopTypeValue"];
    NSString *statesVale;
    if ([self.type isEqualToString:@"1"] ) {
        statesVale = @"3";
    }else if ([self.type isEqualToString:@"2"]){
        statesVale = @"2";
    }else if([self.type isEqualToString:@"3"]){
        statesVale = @"1";
    }
    NSDictionary *param = @{@"token":[LKUserInforTool account].token,@"status":statesVale,@"page":@(page),@"keywords":word,@"userId":[LKUserInforTool account].uid};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopOpenStatusListUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            if (self.page == 0) {
                [self.dataArray removeAllObjects];
            }
            NSArray *dataArra = responseObject[@"data"];
            if (dataArra.count < 15) {
            for (NSDictionary *dic in dataArra) {
                LkMyShopOpenStatusListModel *model = [LkMyShopOpenStatusListModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }
                self.tableView.mj_footer.hidden = YES;
            }else{
                for (NSDictionary *dic in dataArra) {
                    LkMyShopOpenStatusListModel *model = [LkMyShopOpenStatusListModel modelWithDictionary:dic];
                    [self.dataArray addObject:model];
                }
            }
                [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
//谓词搜索过滤
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
        //修改"Cancle"退出字眼,这样修改,按钮一开始就直接出现,而不是搜索的时候再出现
//        searchController.searchBar.showsCancelButton = YES;
//    [searchController.searchBar becomeFirstResponder];
    
//    searchController.searchBar.showsCancelButton = YES;
//        for(id sousuo in [[searchController.searchBar subviews][0] subviews])
//        {
//
//            for (id zz in [sousuo subviews])
//            {
//
//                if([zz isKindOfClass:[UIButton class]]){
//                    UIButton *btn = (UIButton *)zz;
//                    [btn setTitle:@"取消" forState:UIControlStateNormal];
//                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
////                    [btn addTarget:self action:@selector(gobackMainpage) forControlEvents:UIControlEventTouchUpInside];
//                }
//
//
//            }
//        }
//
    self.currentStr = searchController.searchBar.text;
    NSLog(@"updateSearchResultsForSearchController");
    [self getListDataWithStatus:[self.type integerValue] AndWithpage:0 andWords:searchController.searchBar.text];
}



#pragma mark - UISearchControllerDelegate代理
//测试UISearchController的执行过程

- (void)willPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"willPresentSearchController");
}

- (void)didPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"didPresentSearchController");

//    [self.view addSubview:self.searchController.searchBar];
}

- (void)willDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"willDismissSearchController");
}

- (void)didDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"didDismissSearchController");
}

- (void)presentSearchController:(UISearchController *)searchController
{
    NSLog(@"presentSearchController");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
